package cn.zonhar.service.impl;

import cn.zonhar.common.exception.RRException;
import cn.zonhar.service.CityService;
import cn.zonhar.utils.OkHttpUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * 医院服务
 */
@Service("cityService")
public class CityServiceImpl implements CityService {
    @Override
    public JSONArray getCityByUnit(Integer unitId) {
        try {
            Response response = OkHttpUtils.builder()
                    .url("https://www.91160.com/ajax/getunitbycity.html")
                    .addHeader("Referer", "https://www.91160.com")
                    .addHeader("Origin", "https://www.91160.com")
                    .addParam("c",unitId+"")
                    .post(false).syncResponse();
            ResponseBody body = response.body();
            JSONArray objects = JSON.parseArray(body.string());
            if(CollectionUtils.isEmpty(objects)){
                throw new RRException("获取医院列表失败");
            }
            return objects;
        }catch (Exception e){
            throw new RRException("获取医院列表失败");
        }
    }
}
